<?php Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl.'/js/all.js',CClientScript::POS_HEAD)?>
<script type="text/javascript">
    jQuery(function(){

        selectPack({sid:['select','type','position','access','material','params'],attr:['parent_mid','type','position','access','material','params']});

        if(jQuery('#MenuItem_type').val() === 'single-article')
        {
            jQuery('#selectMaterialElement').show();
            jQuery('input#params').val(jQuery('#paramsField').val());
        }
        if(jQuery('#MenuItem_type').val() === 'category-list')
        {
            jQuery('#selectCategoryElement').show();
        }
        //Button Cancel
        jQuery('.form').find('button:last').click(function(e){
            var hostname = location.hostname;
            var protocol = location.protocol;
            var href = location.href;
            if(href === protocol+'//'+hostname+'/menu/backend/create'){

                document.location.replace(protocol+'//'+hostname+'/menu/backend/admin');
            }
            else{
                document.location.replace(protocol+'//'+hostname+'/menu/backend/menuitems');
            }
        });

        jQuery('#selectMaterial').on('show.bs.modal', function () {
            var userScreenHeight;
            userScreenHeight = j(document.body).outerHeight()/2-200;
            jQuery(this).css('margin-top',userScreenHeight+'px');
        });
    });
   //Выводим модальное окно с выбором статей
   function showArticleModal(data)
   {

       jQuery('#ajax_grid').html(data).find('#selectMaterial').modal('show').css('margin-top','100px').find('.modal-dialog').css('width','800px').on('hide.bs.modal', function () {
           jQuery('body').find('.modal-backdrop').remove();
                    });

       jQuery('#selectMaterial').on('hide.bs.modal', function () {
           jQuery('body').find('.modal-backdrop').remove();
                    });

       jQuery('#article-grid tbody a').click(function(e){

           jQuery('#MenuItem_link_type').val(e.target.id);
           jQuery('input#params').val(e.target.text);
           jQuery('#paramsField').val(e.target.id);
           jQuery('#link').val('article/id/'+e.target.id);
           jQuery('#selectMaterial').modal('hide');
                        return false;
                    });
   }
   //Выводим модальное окно с выбором категории
   function showCategorySelect(data)
   {

       jQuery('#ajax_grid').html(data).find('#selectCategory').modal('show').css('margin-top','100px').find('.modal-dialog').css('width','800px').on('hide.bs.modal', function () {
           jQuery('body').find('.modal-backdrop').remove();
                });

       jQuery('#category-grid tbody a').click(function(e){
           jQuery('#MenuItem_link_path').val('category/id/'+e.target.id);
           jQuery('#article_id').val(e.target.text);
           jQuery('#selectCategory').modal('hide');
                    return false;
                });
    }
</script>
<div class="form">
<?php $form=$this->beginWidget('TActiveForm', array(
    'id'=>'menu-form',
    'method'=>'post',
    // Please note: When you enable ajax validation, make sure the corresponding
    // controller action is handling ajax validation correctly.
    // There is a call to performAjaxValidation() commented in generated controller code.
    // See class documentation of CActiveForm for details on this.
    'enableAjaxValidation'=>true,
    'enableClientValidation'=>true,
    'clientOptions'=>array(
        'afterValidate'=>'js:afterValidate',
        'validateOnSubmit'=>true,
        'validateOnType'=>true,
        'validateOnChange'=>true,
      //  'hideErrorMessage'=>true
    ),
)); ?>
<div id="ajax_modal"></div>
<div id="ajax_grid"></div>
<div class="alert alert-info">
    <p class="note"><?php echo Yii::t('MenuModule.base','Fields with')?> <span class="required">*</span> <?php echo Yii::t('MenuModule.base','are required.')?></p>
</div>

    <div class="input-group">
        <?php echo $form->labelEx($model,'status',array('style'=>'width:200px;padding-left:60px')); ?>
    <div id= "status_group" class="btn-group">
        <?php echo CHtml::label(Yii::t('MenuModule.base','Yes'),'MenuItem_status_1',array('class'=>'btn btn-default'));?>
        <?php echo CHtml::label(Yii::t('MenuModule.base','No'),'MenuItem_status_0',array('class'=>'btn btn-default'));?>
    </div>
        <?php echo $form->radioButtonList($model,'status',array(0=>'0',1=>'1'),array('style'=>'display:none','template'=>'{input}')); ?>

        <?php echo $form->error($model,'status',array()); ?>
    </div>

    <div class="input-group">
        <?php echo $form->labelEx($model,'parent_mid',array('style'=>'width:200px;padding-left:30px;')); ?>
        <?php echo $form->dropDownListTb($model,'parent_mid',$model->getParentMenuOptions(),array('id'=>'parent_mid')); ?>
        <?php echo $form->error($model,'parent_mid',array()); ?>
    </div>

    <div style="width:600px">

        <div class="input-group">
            <?php echo CHtml::tag('span',array('class'=>'input-group-addon','style'=>'width:175px'),Yii::t('MenuModule.base','Type menu item'));?>
            <?php echo $form->textField($model,'type',array('id'=>'typeField','class'=>'form-control','size'=>'50','readonly'=>'readonly','disabled'=>'disabled')); ?>
            <?php echo CHtml::activeHiddenField($model,'type',array('class'=>'form-control','size'=>'50','readonly'=>'readonly')); ?>
            <?php echo CHtml::openTag('span',array('class'=>'input-group-btn'));?>
            <?php echo CHtml::ajaxLink("<i class='glyphicon glyphicon-list inner'></i>".Yii::t('MenuModule.base','Select'),CController::createUrl('backend/ajax_modal'),array('type'=>'POST','data'=>array('update'=>true),'update'=>'#ajax_modal'),array('class'=>'btn btn-primary','type'=>'button','style'=>'height:34px;top:0;border-bottom-left-radius:0;border-top-left-radius:0'));?>
            <?php echo CHtml::closeTag('span');?>
            <?php echo $form->error($model,'type',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
        </div>
    </div>

     <div id="selectCategoryElement" style="width:600px;display:none">
        <div class="input-group">
            <?php echo $form->labelEx($model,'params',array('style'=>'width:200px;padding-left:50px;')); ?>
            <?php echo $form->dropDownListTb($model,'params',$model->getCategories(),array('id'=>'params')); ?>
            <?php echo $form->error($model,'params',array()); ?>
        </div>
    </div>

    <div id="selectMaterialElement" style="width:600px;display:none">

        <div class="input-group">
            <?php echo CHtml::tag('span',array('class'=>'input-group-addon','style'=>'width:175px;padding-left:30px'),Yii::t('MenuModule.base','Choice of material'));?>
            <?php echo CHtml::textField('params','',array('class'=>'form-control','size'=>'50','placeholder'=>Yii::t('MenuModule.base','Material is not selected'),'readonly'=>'readonly','disabled'=>'disabled')); ?>
            <?php echo CHtml::activeHiddenField($model,'params',array('id'=>'paramsField')); ?>
            <?php echo CHtml::openTag('span',array('class'=>'input-group-btn'));?>
            <?php echo CHtml::ajaxLink("<i class='glyphicon glyphicon-file inner'></i>".Yii::t('MenuModule.base','Select'),CController::createUrl('backend/ajax_article_grid'),array('type'=>'POST','data'=>array('update'=>true),'success'=>'js:showArticleModal'),array('class'=>'btn btn-primary','type'=>'button','style'=>'height:34px;top:0'));?>
            <?php echo CHtml::closeTag('span');?>
            <?php echo $form->error($model,'params',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
        </div>
    </div>

    <div class="input-group" style="display: float:left">
        <?php echo $form->labelTb($model,'title',array('style'=>'width:150px')); ?>
        <?php echo $form->textFieldTb($model,'title',array('size'=>50,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'title',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>


    <div class="input-group">
        <?php echo $form->labelTb($model,'link_path',array('style'=>'width:150px')); ?>
        <?php echo $form->textFieldTb($model,'link_path',array('size'=>50,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'link_path',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>

    <div class="input-group">
        <?php echo $form->labelEx($model,'position',array('style'=>'width:200px;padding-left:30px;')); ?>
        <?php echo $form->dropDownListTb($model,'position',$model->getPositionOptions(),array('id'=>'position')); ?>
        <?php echo $form->error($model,'position',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>

    <div class="input-group">
        <?php echo $form->labelEx($model,'access',array('style'=>'width:200px;padding-left:30px;')); ?>
        <?php echo $form->dropDownListTb($model,'access',$model->getAccessOptions(),array('id'=>'access')); ?>
        <?php echo $form->error($model,'access',array('afterValidateAttribute'=>'js:afterValidateAttribute')); ?>
    </div>

    <div>
        <?php echo CHtml::openTag('button',array('type'=>'submit','class'=>'btn btn-success','name'=>'yt0')); ?>
        <i class='glyphicon glyphicon-floppy-save'></i>
        <?php echo $model->isNewRecord ? Yii::t('MenuModule.base','Create') : Yii::t('MenuModule.base','Save'); ?>
        <?php echo CHtml::closeTag('button'); ?>

        <?php echo CHtml::openTag('button',array('type'=>'button','class'=>'btn btn-default')); ?>
        <i class='glyphicon glyphicon-remove'></i>
        <?php echo Yii::t('MenuModule.base','Cancel'); ?>
        <?php echo CHtml::closeTag('button'); ?>
    </div>
<?php $this->endWidget('TActiveForm');?>
</div>













